<?php 
App::import('Vendor', 'phpmailer', array('file' => 'phpmailer' . DS . 'class.phpmailer.php'));
class MailerComponent extends phpmailer{
    
    var $from               = 'admin@kleepon.com';
    var $fromName           = "Kleepon Administrator";
    var $sitePrefix         = 'http://www.kleepon.com';
    var $useSMTPAuth        = true;
    var $smtpUserName       = 'admin@kleepon.com';
    var $smtpPassword       = 'kleepon4ever$rocks';
    var $smtpHostNames      = 'mail.kleepon.com';
    var $text_body          = null;
    var $html_body          = null;
    var $to                 = null;
    var $toName             = null;
    var $subject            = null;
    var $cc                 = null;
    var $bcc                = null;
    var $template           = 'email/default';
    var $attachments        = null;
    
    var $controller;
    
    function startup( & $controller){
       $this->controller = & $controller;
    }
    
    /**
    * Helper function to generate the appropriate template location
    *
    * @return string CakePHP location of the template file
    * @param object $template_type
    */
    function templateLocation($template_type){
        return ('..'.DS.strtolower($this->controller->name).DS.$this->template.$template_type);
    }
    
    /**
    * Renders the content for either html or text of the email
    *
    * @return string Rendered content from the associated template
    * @param object $type_suffix
    */
    function bodyContent($type_suffix){
        $temp_layout = $this->controller->layout; // store the current controller layout
        
        if ($type_suffix == 'html'){
            $this->controller->layout = '..'.DS.'email';
        }else{
            $this->controller->layout = '';
        }
        
        $mail = $this->controller->render($this->templateLocation('_'.strtolower($type_suffix)));
        $this->controller->output = str_replace($mail, '', $this->controller->output);
        $this->controller->layout = $temp_layout; // restore the controller layout
        return $mail;
    }
    
    function attach($filename, $asfile = ''){
        if ( empty($this->attachments)){
            $this->attachments = array ();
            $this->attachments[0]['filename'] = $filename;
            $this->attachments[0]['asfile'] = $asfile;
        }else{
            $count = count($this->attachments);
            $this->attachments[$count+1]['filename'] = $filename;
            $this->attachments[$count+1]['asfile'] = $asfile;
        }
    }
    
    function send(){
        
        $mail = new phpmailer();
        $mail->IsSMTP();
        
        $mail->Host         = $this->smtpHostNames;
        //$mail->Port         = $this->smtpPort;
        $mail->SMTPAuth     = true;
        $mail->Username     = $this->smtpUserName;
        $mail->Password     = $this->smtpPassword;
        
        $mail->From         = $this->from;
        $mail->FromName     = $this->fromName;
        $mail->Subject 	    = $this->subject;
        
        $mail->AddAddress($this->to);
        //$mail->AddReplyTo($this->from, $this->fromName);
        
        $mail->CharSet      = 'UTF-8';
        $mail->WordWrap     = 80; // set word wrap to 50 characters
        
        if(!empty($this->html_body)){
            $mail->IsHTML(true); // set email format to HTML
            $mail->Body     = $this->html_body;
        }else{
            $mail->Body     = $this->text_body;	
        }
        //$result = $mail->Send();
        return $mail->Send() ? true : false;
    }

    
    function _sendMailRoutine($to,$subject,$template) {		
		
		$this->to = $to;
		$this->subject = $subject;
		$this->html_body = $template;
        
		return $this->send() ? true : false;
	}
}
?>